package com.xiaobug.demo.alipay.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xiaobug.demo.R;
import com.xiaobug.demo.alipay.MenuItem;
import com.xiaobug.demo.databinding.AlipayRvItemBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单编辑列表的适配器
 * Created by ljb on 2018/5/17.
 */

public class MenuRecyclerviewGridEditAdapter extends RecyclerView.Adapter<BindingViewHolder> {
    private List<MenuItem> itemData;
    private final LayoutInflater mInflater;
    private ItemTouchHelper itemTouchHelper;

    public MenuRecyclerviewGridEditAdapter(Context c) {
        mInflater = LayoutInflater.from(c);

    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BindingViewHolder<>(DataBindingUtil.inflate(mInflater, R.layout.alipay_rv_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final BindingViewHolder holder, int position) {
        ((AlipayRvItemBinding) holder.getBinding()).setItem(itemData.get(position));
        holder.getBinding().getRoot().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                itemTouchHelper.startDrag(holder);
                return true;
            }
        });
    }


    @Override
    public int getItemCount() {
        return itemData == null ? 0 : itemData.size();
    }

    public void setData(List<MenuItem> hearData) {
        if (itemData == null)
            itemData = new ArrayList<>();
        itemData.clear();
        itemData.addAll(hearData);
    }

    public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
        this.itemTouchHelper = itemTouchHelper;
    }
}
